//const data = require('../../utils/data.js');
//获取应用实例
const app = getApp();
const db = wx.cloud.database();
Page({
  data: {
    hideLoading:false,
    banner: [],
    homeList: []
  },
  onShareAppMessage: function () {
    return {
      title: 'JEEXJJ开源社区',
      desc: '',
      path: '/pages/index/index'
    }
  },

  getIndexData: function () {
    let that = this;
    //滚动屏

    db.collection('home_news').where({'showBanner':true}).get().then(res => {
      that.setData({ banner: res.data });
    });

    db.collection('home_news').get().then(res => {
      that.setData({ homeList: res.data });
    });

    setTimeout(function () {
      that.setData({
        hideLoading: true
      });
    },300);
  },

  gotoArticle4Banner:function(e)
  {
    var idx = e.currentTarget.dataset.index;
    var item = this.data.banner[idx]; 
    let data = JSON.stringify(item);
    wx.navigateTo({
      url: '/pages/article/article?data='+data
    })
  },

  gotoArticle: function (e) {
    var idx = e.currentTarget.dataset.index;
    var item = this.data.homeList[idx];
    let data = JSON.stringify(item);
    wx.navigateTo({
      url: '/pages/article/article?data=' + data
    })
  },
  
  onLoad: function (options) {
    //查询首页数据
    this.getIndexData();

    if (null != app.globalData.openid)
    {
      return;
    }

    //用户登陆记录openid
    // 调用云函数
    wx.cloud.callFunction({
      name: 'login',
      data: {},
      success: res => {
        console.log('[云函数] [login] user openid: ', res.result.openid.openId)
        app.globalData.openid = res.result.openid.openId;
        if ("opyCK5bPsmGzN7dVTTVkuCtUifps" == res.result.openid.openId)
        {
          app.globalData.isAdmin = true;
        }
        console.info(app.globalData);
      },
      fail: err => {
        console.error('[云函数] [login] 调用失败', err)
      }
    })

  },
  onPullDownRefresh: function () {
    this.setData({hideLoading:false });
    this.getIndexData();
  },
  onReady: function () {
    // 页面渲染完成
  },
  onShow: function () {
    // 页面显示
  },
  onHide: function () {
    // 页面隐藏
  },
  onUnload: function () {
    // 页面关闭
  },
})
